package com.loong.ems.controller;

import com.github.pagehelper.PageInfo;
import com.loong.ems.domain.entity.Employee;
import com.loong.ems.domain.vo.resp.Result;
import com.loong.ems.service.EmployeeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.List;

/**
 * @author 龙勇-229970722
 * @date 2025-01-02 8:52
 * @description: 员工信息管理
 */

@RestController
@RequestMapping("/employee")
@Log4j2
@CrossOrigin
@Tag(name = "员工信息管理 API", description = "员工信息管理 API")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 获取员工列表
     *
     * @return
     */
    @Operation(summary = "获取员工列表")
    @GetMapping("/list")
    public Result<List<Employee>> list() {
        return Result.success(employeeService.list());
    }

    /**
     * 分页获取员工列表
     *
     * @param pageNum  当前页码
     * @param pageSize 每页大小
     * @return 分页结果
     */
    @Operation(summary = "分页获取员工列表")
    @GetMapping("/listByPage")
    public Result<PageInfo<Employee>> listByPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo<Employee> pageInfo = employeeService.listByPage(pageNum, pageSize);
        return Result.success(pageInfo);
    }

    /**
     * 离职员工
     *
     * @param id 员工ID
     * @return
     */
    @Operation(summary = "离职员工")
    @GetMapping("/{id}")
    public Result<String> delete(@PathVariable("id") Long id) {
        employeeService.deleteById(id);
        return Result.success("删除成功");
    }

    /**
     * 更新员工信息
     *
     * @param employee 员工信息
     * @return
     */
    @Operation(summary = "更新员工信息")
    @PostMapping("/update")
    public Result<String> updateByEmployee(@RequestBody Employee employee) {
        employeeService.updateByEmployee(employee);
        return Result.success("更新成功");
    }

    /**
     * 添加员工信息
     *
     * @param employee 员工信息
     * @return
     */
    @Operation(summary = "添加员工信息")
    @PostMapping("/add")
    public Result<String> addByEmployee(@RequestBody Employee employee) {
        employeeService.addByEmployee(employee);
        return Result.success("添加成功");
    }

    /**
     * 查看离职员工
     *
     * @return
     */
    @Operation(summary = "查看离职员工")
    @GetMapping("/listLeave")
    public Result<List<Employee>> listLeave() {
        return Result.success(employeeService.listLeave());
    }

    /**
     * 恢复员工
     *
     * @param id 员工ID
     * @return
     */
    @Operation(summary = "恢复员工")
    @PostMapping("/recover/{id}")
    public Result<String> recover(@PathVariable("id") Long id) {
        employeeService.recover(id);
        return Result.success("恢复成功");
    }

    /**
     * 根据字段模糊查询员工列表
     */
    @Operation(summary = "根据字段模糊查询员工列表")
    @GetMapping("/search/{str}")
    public Result<PageInfo<Employee>> search(
            @PathVariable("str") String str,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        List<Employee> pageInfo = employeeService.search(str, pageNum, pageSize);
        return Result.success(new PageInfo<>(pageInfo));
    }

    /**
     * 导出员工信息
     * @return
     */
    @Operation(summary = "导出员工信息")
    @GetMapping("/exportEmployees")
    public ResponseEntity<FileSystemResource> exportEmployees() {
        // 设置要导出的文件路径（建议在服务器的临时目录中生成文件）
        String filePath = "employees.xlsx";  // 在项目根目录下保存文件

        // 调用服务生成 Excel 文件
        employeeService.exportEmployeeData(filePath);

        // 创建文件对象
        File file = new File(filePath);

        // 检查文件是否存在
        if (!file.exists()) {
            return ResponseEntity.notFound().build();  // 返回 404 如果文件未找到
        }

        // 设置响应头
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"");
        headers.add(HttpHeaders.CONTENT_TYPE, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); // 设置为 Excel 文件类型

        // 返回响应，允许用户下载文件
        return ResponseEntity.ok()
                .headers(headers)
                .body(new FileSystemResource(file));  // 将文件作为响应体返回
    }


}
